package com.astrofizzbizz.raspberrypicamera.client;

import com.astrofizzbizz.raspberrypicamera.shared.RaspberryPiException;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LaunchTimeLapsePanel extends VerticalPanel
{
	private RaspberryPiCamera entryPoint;
	TextBox intervalTimeTextBox = new TextBox();
	TextBox maxTimeTextBox = new TextBox();
	TextBox numImagesTextBox = new TextBox();
	private Button startTimeLapseButton = new Button("Start Time Lapse");

	public LaunchTimeLapsePanel(RaspberryPiCamera entryPoint)
	{
		super();
		this.entryPoint = entryPoint;
		intervalTimeTextBox.setText("3");
		maxTimeTextBox.setText("30");
		numImagesTextBox.setText("15");
		Grid grid = new Grid(4, 3);
		grid.setWidget(0, 0, new Label("Interval Time"));
		grid.setWidget(1, 0, new Label("Maxmium  Time"));
		grid.setWidget(2, 0, new Label("Maximum No. Images"));
		grid.setWidget(0, 1, intervalTimeTextBox);
		grid.setWidget(1, 1, maxTimeTextBox);
		grid.setWidget(2, 1, numImagesTextBox);
		grid.setWidget(0, 2, new Label("secs"));
		grid.setWidget(1, 2, new Label("secs"));
		grid.setWidget(2, 2, new Label(""));
		grid.setWidget(3, 0, startTimeLapseButton);
		add(grid);
		startTimeLapseButton.addClickHandler(new StartTimeLapseButtonClickHandler(this));
	}
	private class StartTimeLapseButtonClickHandler implements ClickHandler
	{
		private LaunchTimeLapsePanel launchTimeLapsePanel;
		private StartTimeLapseButtonClickHandler(LaunchTimeLapsePanel launchTimeLapsePanel)
		{
			this.launchTimeLapsePanel = launchTimeLapsePanel;
		}
		@Override
		public void onClick(ClickEvent event) 
		{
			launchTimeLapsePanel.setVisible(false);
			String debugResponse = "";
			try
			{
				String switchSettings = entryPoint.getMyTabLayoutPanel().getCameraSettingsPanel().getSwitchSettings();
				entryPoint.getStatusTextArea().addStatus("Started Time Lapse - go grab Snickers");
				entryPoint.getRaspberryPiService().startTimeLapse(
						intervalTimeTextBox.getText(), 
						maxTimeTextBox.getText(), 
						numImagesTextBox.getText(), 
						switchSettings,
						entryPoint.isDebug(), debugResponse, new StartTimeLapseCallback());
			}
			catch (RaspberryPiException e)
			{
				
				launchTimeLapsePanel.setVisible(true);
				entryPoint.getMyTabLayoutPanel().selectTab(entryPoint.getMyTabLayoutPanel().getCameraSettingsPanelTabId());
			}
		}
		class StartTimeLapseCallback implements AsyncCallback<String>
		{
			@Override
			public void onFailure(Throwable caught) 
			{
				entryPoint.getStatusTextArea().addStatus("Error on Start Time Lapse: " + caught.getMessage());
			}
			@Override
			public void onSuccess(String result) 
			{
			}

			
		}

	}
}
